/**
 * grid面板，设置表格的显示样式
 * @author mayonghua
 * @param config
 * @return
 */
var GridPanel = function(config){
	this.titleTable = config.titleTable;
	this.dataTable = config.dataTable;
	this.width = config.width;
	this.title = config.title;
	this.toolBar = config.toolBar;
	this.collapse = config.collapse;
	this.init();
}
GridPanel.prototype = {
	init : function(){
		var entity = this;
		entity.drawGrid();
		entity.drawPanel();
		entity.windowResize();
		$(window).resize(function(){
			entity.windowResize();
		});
		//是否和父容器合并边框
		if(entity.collapse){
			$("div").css("border","none");
		}
	},
	//绘制表格
	drawGrid : function(){
		var entity = this;
		if(entity.width==""||entity.width==null){
			return;
		}
		//获得标题和内容
		var titleTable = $("#"+entity.titleTable);
		var dataTable = $("#"+entity.dataTable);
		var tableWidth = titleTable.width();//table的总宽度
		var length = entity.width.length;
		for(var i=0;i<length;i++){
			var tdWidth = entity.width[i];
			//设置标题table中每个td的宽度
			var titleTd = $("#"+entity.titleTable).find("tr").eq(0).find("td").eq(i);
			var dataTd = $("#"+entity.dataTable).find("tr").eq(0).find("td").eq(i);
			//如果没有指定宽度，则说明是该TD是填充剩余的空间
			if(!isNaN(tdWidth)){
				titleTd.width(tdWidth);
				dataTd.width(tdWidth);
			}
		}
	},
	drawPanel : function(){
		var entity = this;
	  //绘制一个表格面板
		var div = [];
			div.push('<div class="gridPanel" id="gridPanel">');
			//设置标题
			if(entity.title){
			div.push('<div class="head">');
			div.push('<div class="title">'+entity.title+'</div>');
			div.push('</div>');
			}
			//设置toolbar			
			if(entity.toolBar){
			div.push('<div class="toolBar" id="toolBar"></div>');
			}
			div.push('</div>');
		//将表格头部添加到表格的前面
			$("#"+entity.titleTable).before($(div.join('')));
		//把table元素包裹起来
			$('table').wrapAll('<div class="content"></div>');
			$('#'+entity.dataTable).wrapAll('<div class="dataContent"></div>');
	},
	windowResize : function(){
		var entity = this;
		var height = $('body').height();
		var gridPanelHeight = $("#gridPanel").height();
		var titleTableHeight = $("#"+entity.titleTable).height();
		var tableDataHeight = height-gridPanelHeight-titleTableHeight*2;
		$('.dataContent').height(tableDataHeight);
		$('.dataContent').width($("#gridPanel").width());
	}
}